package cn.qvtu.game.model;

import cn.qvtu.game.control.EnemyController;
import cn.qvtu.game.util.StaticValue;

import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Enemy implements Runnable {
    protected int x; // x坐标
    protected int y; // y坐标
    protected BufferedImage ShowImage; // 图像
    protected Thread thread; // 线程
    protected double moving = 0.0; // 在敌人目录里循环
    protected int status = 1; // 状态 默认向右站立
    protected int jumpForce = 0; // 跳跃力度
    protected int jumpMoveDirection = 0; // 跳跃方向 (0 垂直 1 左上 2 右上)
    protected BackGround background;
    private Thread controlThread; // 控制线程

//    protected ArrayList<BufferedImage> LeftStandImages; // 左站立图集
//    protected ArrayList<BufferedImage> RightStandImages; // 右站立图集
//    protected ArrayList<BufferedImage> LeftRunImages;   // 左跑图集
//    protected ArrayList<BufferedImage> RightRunImages;  // 右跑图集
//    protected ArrayList<BufferedImage> LeftJumpImages;  // 左跳跃图集
//    protected ArrayList<BufferedImage> RightJumpImages; // 右跳跃图集
//    protected ArrayList<BufferedImage> LeftAttackImages; // 左攻击图集
//    protected ArrayList<BufferedImage> RightAttackImages; // 右攻击图集

    public Enemy(int x, int y,BackGround background) {
        this.x = x;
        this.y = y;
        this.background = background;
        this.thread = new Thread(this);
        this.thread.start();
        controlThread = new Thread(new EnemyController(this));
        controlThread.start();
//        setImageList(); // 调用设置图像的方法
    }

    // 设置敌人图像
//    protected void setImageList() {
//        LeftStandImages = (ArrayList<BufferedImage>) StaticValue.LeftEnemyImages.subList(14, 19);
//        RightStandImages = (ArrayList<BufferedImage>) StaticValue.RightEnemyImages.subList(14, 19);
//        LeftRunImages = (ArrayList<BufferedImage>) StaticValue.LeftEnemyImages.subList(0, 6);
//        RightRunImages = (ArrayList<BufferedImage>) StaticValue.RightEnemyImages.subList(0, 6);
//        LeftAttackImages = (ArrayList<BufferedImage>) StaticValue.LeftEnemyImages.subList(6, 14);
//        RightAttackImages = (ArrayList<BufferedImage>) StaticValue.RightEnemyImages.subList(6, 14);
//        LeftJumpImages = (ArrayList<BufferedImage>) StaticValue.LeftEnemyImages.subList(4, 5);
//        RightJumpImages = (ArrayList<BufferedImage>) StaticValue.RightEnemyImages.subList(4, 5);
//    }

    // 添加其他行为，例如移动、攻击等

    @Override
    public void run() {
        // 线程更新敌人状态
        while (true) {
            try {
                switch (this.status) {
                    case 1:
                        // 敌人在右侧-站立
                        if (this.moving > 4) { // 循环站立的图像
                            this.moving = 0;
                        }
                        this.ShowImage = StaticValue.RightEnemyImages.get((int) this.moving); // 更新图像
                        this.moving += 0.2;
                        break;
                    case 2:
                        // 敌人在右侧-跑
                        if (this.moving < 5 || this.moving > 8) { // 循环奔跑的图像
                            this.moving = 5;
                        }
                        this.ShowImage = StaticValue.RightEnemyImages.get((int) this.moving); // 更新图像
                        this.moving += 0.2;
                        break;
                    case 3:
                        // 敌人在右侧-跳跃
                        this.moving = 6;
                        this.ShowImage = StaticValue.RightEnemyImages.get((int) this.moving); // 更新图像
                        break;
                    case 4:
                        // 敌人在右侧-攻击
                        if (this.moving < 9 || this.moving > 11) { // 循环攻击的图像
                            this.moving = 9;
                        }
                        this.ShowImage = StaticValue.RightEnemyImages.get((int) this.moving); // 更新图像
                        this.moving += 0.5;
                        if (this.moving == 11) {
                            this.status = 1; // 攻击后恢复站立状态
                        }
                        break;
                    case -1:
                        // 敌人在左侧-站立
                        if (this.moving > 4) { // 循环站立的图像
                            this.moving = 0;
                        }
                        this.ShowImage = StaticValue.LeftEnemyImages.get((int) this.moving); // 更新图像
                        this.moving += 0.2;
                        break;
                    case -2:
                        // 敌人在左侧-跑
                        if (this.moving < 5 || this.moving > 8) { // 循环奔跑的图像
                            this.moving = 5;
                        }
                        this.ShowImage = StaticValue.LeftEnemyImages.get((int) this.moving); // 更新图像
                        this.moving += 0.2;
                        break;
                    case -3:
                        // 敌人在左侧-跳跃
                        this.moving = 6;
                        this.ShowImage = StaticValue.LeftEnemyImages.get((int) this.moving); // 更新图像
                        break;
                    case -4:
                        // 敌人在左侧-攻击
                        if (this.moving < 9 || this.moving > 11) { // 循环攻击的图像
                            this.moving = 9;
                        }
                        this.ShowImage = StaticValue.LeftEnemyImages.get((int) this.moving); // 更新图像
                        this.moving += 0.5;
                        if (this.moving == 11) {
                            this.status = -1; // 攻击后恢复站立状态
                        }
                        break;
                    default:
                        break;
                }
                // 人物移动
                moveXY();

                // 边界检测
                if (this.x < -100) {
                    this.x = -100;
                }
                if (this.x > 750) {
                    this.x = 750;
                }

                Thread.sleep(50); // 每隔50毫秒更新一次状态
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void moveXY() {
        switch (this.status) {
            case 1: case -1:
                // 站立
                break;
            case 2:
                // 向右-跑
                this.x += 10;
                break;
            case 3:case -3:
                // 跳跃
                if (this.jumpMoveDirection > 0){
                    this.x += 10;// 向右跳跃
                }
                else if (this.jumpMoveDirection < 0){
                    this.x -= 10;// 向左跳跃
                }
                this.y -= this.jumpForce;
                if (this.y > 305){
                    this.y = 305;// 跳跃高度限制

                    if (this.jumpMoveDirection > 0){
                        this.status = 2; // 跳跃后恢复向右跑状态
                    }
                    else if (this.jumpMoveDirection < 0){
                        this.status = -2; // 跳跃后恢复向左跑状态
                    }
                    else{
                        this.status = this.status>0?1:-1; // 跳跃后恢复站立状态
                    }
                }
                jumpForce--;
                break;
            case 4:case -4:
                // 攻击
                break;
            case -2:
                // 向左-跑
                this.x -= 10;
                break;
            default:
                break;
        }
    }

    // 其他相关方法，例如获取坐标、状态等
    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public BackGround getBackground() {
        return background;
    }

    public void setBackground(BackGround background) {
        this.background = background;
    }

    public BufferedImage GetShowImage() {
        if (ShowImage == null) {
            ShowImage = StaticValue.LeftEnemyImages.get(0);
        }
        return ShowImage;
    }

    public void SetShowImage(BufferedImage showImage) {
        this.ShowImage = showImage; // 设置显示的图像
    }

    public void Dead() {
        this.background.getEnemyList().remove(this); // 从背景中移除敌人
    }
}
